<%@page import="java.io.FileInputStream"%>
<%@page import="wt.httpgw.HTTPRequest"%>
<%@page import="ext.tianma.upload.creatDocuments"%>
<%@page import="wt.doc.WTDocument"%>
<%@page import="wt.fc.ReferenceFactory"%>
<%@page import="java.io.File"%>
<%@page import="java.io.InputStream"%>
<%@page import="org.springframework.web.multipart.MultipartFile"%>
<%@page import="org.springframework.web.multipart.MultipartResolver"%>
<%@page import="org.springframework.web.multipart.commons.CommonsMultipartResolver"%>
<%@page import="org.springframework.web.multipart.MultipartHttpServletRequest"%>
<%@page import="ext.tianma.upload.SmartUpload"%>
<%@page import="wt.util.WTProperties" %>
<%@page import="org.apache.commons.io.FileUtils" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ImportDocInfor</title>
</head>
<body>


	<%
			String docKey = request.getParameter("docKey");
			String wid = request.getParameter("wid");
			String values = request.getParameter("values");
			
			
			SmartUpload mySmartUpload = new SmartUpload();
			WTDocument doc = null;
			String name = "";
			String showName = "";
			File saveFile = null;
			try {
				System.out.println("---pageContext:"+pageContext);
			    mySmartUpload.initialize(pageContext);  //初始化
			    mySmartUpload.upload();
			    ext.tianma.upload.File myFile = mySmartUpload.getFiles().getFile(0);
			    System.out.println("---myFile:"+myFile);
			    if (myFile.isMissing()){
				%>
			<SCRIPT language=javascript>
			                alert("文档is miss！");
			                window.close();
			            </SCRIPT>
			<% 
		    }else {
		        String ext = myFile.getFileExt(); //取得后缀名
		        name = myFile.getFileName();//取得文件名
		        
		        String wthome = (String) (WTProperties.getLocalProperties()).getProperty("wt.home", "");
		        //String saveurl = wthome + File.separator + "temp" + File.separator;
		        String saveurl = wthome + File.separator + "codebase" + File.separator + "temp" + File.separator + "uploadFiles" + File.separator;
		        saveurl += name ; 
		        myFile.saveAs(saveurl, mySmartUpload.SAVE_PHYSICAL);
		        saveFile = new File(saveurl);
		        int lastFileSeperatorIndex = name.lastIndexOf(".");
		        showName = name.substring(0, lastFileSeperatorIndex);
		        doc = creatDocuments.createDoc(wid, docKey, showName, values, saveFile);
		    }
			
			
			
		} catch (java.lang.StringIndexOutOfBoundsException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (doc != null) {
	%>
            <SCRIPT language=javascript>
                alert("文档上传成功");
                self.opener.location.reload();
                //window.close();
            </SCRIPT>
            <%
		} else {
			%>
            <SCRIPT language=javascript>
                alert("文档上传失败，请联系管理员");
                window.close();
            </SCRIPT>
            <%
			
		}
	%>
</body>
</html>
